package com.atjava.webspider.services;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.atjava.webspider.gui.task.NewTaskGui;
import com.atjava.webspider.model.Output;
import com.atjava.webspider.model.Page;
import com.atjava.webspider.model.Replace;
import com.atjava.webspider.model.Resource;
import com.atjava.webspider.model.Site;
import com.atjava.webspider.model.Task;
import com.atjava.webspider.model.Variable;
import com.atjava.webspider.util.DateUtil;
import com.atjava.webspider.util.TableUtil;

public class TableService {
	
	private static TableService service;
	
	private TableService (){}
	
	public static TableService getInstance(){
		if(service == null){
			service = new TableService();
		}
		return service;
	}
	
	public List<String []> getSiteTable(List<Site> sites)throws Exception{
		return TableUtil.getTableData(sites, "name","charset","pageNames","replaceNum");
	}
	
	public String [] getSiteRow(Site st)throws Exception{
		return TableUtil.getRow(st,  "name","charset","pageNames","replaceNum");
	}
	
	public String [] getPageRow(Page p)throws Exception{
		return TableUtil.getRow(p,  "name","level","varNum");
	}
	
	public List<String []> getPageTable(List<Page> pages)throws Exception{
		return TableUtil.getTableData(pages, "name","level","varNum");
	}
	
	public String [] getOutputRow(Output out)throws Exception{
		return TableUtil.getRow(out,  "name","path","typeName","urlRegexp");
	}
	
	public List<String []> getOutputTable(List<Output> outputs)throws Exception{
		return  TableUtil.getTableData(outputs, "name","path","typeName","urlRegexp");
	}
	
	public List<String []> getVarTable(List<Variable> vars)throws Exception{
		return TableUtil.getTableData(vars, "name","regexp","groupNum");
	}
	
	public List<String []> getResTable(List<Resource> res)throws Exception{
		return TableUtil.getTableData(res, "name","path","scope");
	}
	
	public String [] getResRow(Resource res)throws Exception{
		String [] resRow = TableUtil.getRow(res, "name","path","scope");
		resRow[2] = NewTaskGui.SCOPE_COMBOBOX[res.getScope()];
		return resRow;
	}
	
	public List<String []> getReplaceTable(List<Replace> reps)throws Exception{
		return TableUtil.getTableData(reps, "regexp","groupNum","replacement");
	}
	
	public String [] getReplaceRow(Replace rep)throws Exception{
		return TableUtil.getRow(rep, "regexp","groupNum","replacement");
	}
	
	public String [] getTaskRow(Task task)throws Exception{
		return TableUtil.getRow(task, "id","stateName","name","dataNum","startTimeStr","passedTime");
	}
	
	public String [] getVarRow(Variable var)throws Exception{
		return TableUtil.getRow(var, "name","regexp","groupNum");
	}
	
	public List<String []> getTaskTable(List<Task> tasks)throws Exception{
		return TableUtil.getTableData(tasks,"id","stateName","name","dataNum","startTimeStr","passedTime");
	}
	
	public List<String []> getDataTable(List<File> data){
		
		List<String []> table = new ArrayList<String[]>();
		for (int i = 0; i < data.size(); i++) {
			File f = data.get(i);
			if(f.isDirectory())
				continue;
			if(f.getName().length() < 25)
				continue;
			String [] row = new String[4];
			row[0] = f.getName();
			row[1] = f.getParent();
			row[2] = Math.ceil(new Double(f.length()/1024)) + "K";
			row[3] = DateUtil.formatDataCN(new Date(f.lastModified()));
			table.add(row);
		}
		return table;
	}
}
